#include "OptionsMenuState.h"

#include "Game.h"
#include "MainMenuState.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "ProfileManager.h"
#include "BitmapFont.h"
#include "Hologram.h"
#include "AudioManager.h"

OptionsMenuState* OptionsMenuState::pInstance = nullptr;
/*static*/ OptionsMenuState* OptionsMenuState::GetInstance( void )
{
	// Allocate the static pointer if necessary
	if( pInstance == nullptr )
		pInstance = new OptionsMenuState;

	// Return the singleton
	return pInstance;
}

/*static*/ void OptionsMenuState::DeleteInstance( void )
{
	delete pInstance;
	pInstance = nullptr;
}


// Update
void OptionsMenuState::Update(float deltaTime)
{
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;
	if(inputDelay > 0.0f)
		inputDelay -= deltaTime;


	if(dI->KeyPressed(DIK_ESCAPE) || dI->JoystickButtonPressed(7))
	{
		ProfileManager::GetInstance()->saveProfiles();
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		return;
	}
	if(ProfileManager::GetInstance()->getCurProfile().getControlScheme() == 5)
	{
		if(dI->JoystickButtonPressed(6))
		{
			ProfileManager::GetInstance()->saveProfiles();
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return;
		}
	}
	if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
	{
		if(cursor >= 100)
		{
			cursor = 0;
		}
		else
		{
			switch(cursor)
			{
			case 0:
				cursor = 100;
				break;
			case 1:
				cursor = 101;
				break;
			case 2:
				cursor = 102;
				break;
			case 3:
				ProfileManager::GetInstance()->getCurProfile().toggleIsWindowed();
				Game::GetInstance()->SetIsWindowed(ProfileManager::GetInstance()->getCurProfile().getIsWindowed());
				break;
			case 4:
				cursor = 104;
				break;
			case 5:
				ProfileManager::GetInstance()->saveProfiles();
				Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
				break;
			}
			return;
		}
	}
	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursor < 100)
		{
			if(cursor > 0)
				--cursor;
			else
				cursor = 5;
		}
	}
	else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursor < 100)
		{
			if(cursor < 5)
				++cursor;
			else
				cursor = 0;
		}
	}
	if ( (dI->KeyDown(DIK_RIGHT) || dI->KeyDown(DIK_D) || dI->JoystickDPadDown(DIR_RIGHT)) && inputDelay <= 0 || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 && controllerDelay <= 0) )
	{
		if(cursor == 100)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getMasterVolume() < 100)
			{
				pm->getCurProfile().setMasterVolume(pm->getCurProfile().getMasterVolume() + 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;
			}
		}
		else if (cursor == 101)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getMusicVolume() < 100)
			{
				pm->getCurProfile().setMusicVolume(pm->getCurProfile().getMusicVolume() + 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;
			}
		}
		else if (cursor == 102)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getSFXVolume() < 100)
			{
				pm->getCurProfile().setSFXVolume(pm->getCurProfile().getSFXVolume() + 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;
				AudioManager::GetInstance()->playSFX("thermo_shoot");
			}
		}
	}
	if ((dI->KeyDown(DIK_LEFT) || dI->KeyDown(DIK_A) || dI->JoystickDPadDown(DIR_LEFT))  && inputDelay <= 0.0f || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 && controllerDelay <= 0))
	{
		if(cursor == 100)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getMasterVolume() > 0)
			{
				pm->getCurProfile().setMasterVolume(pm->getCurProfile().getMasterVolume() - 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;
			}
		}
		else if (cursor == 101)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getMusicVolume() > 0)
			{
				pm->getCurProfile().setMusicVolume(pm->getCurProfile().getMusicVolume() - 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;

			}
		}
		else if (cursor == 102)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			if(pm->getCurProfile().getSFXVolume() > 0)
			{
				pm->getCurProfile().setSFXVolume(pm->getCurProfile().getSFXVolume() - 2);
				controllerDelay = 0.1f;
				inputDelay = 0.05f;

				AudioManager::GetInstance()->playSFX("thermo_shoot");
			}
		}
	}

	if(dI->KeyPressed(DIK_LEFT) || dI->KeyPressed(DIK_A) || dI->JoystickDPadPressed(DIR_LEFT) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 && controllerDelay <= 0))
	{
		if(cursor == 104)
			controllerDelay = 0.3f;
		if (cursor == 104)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			int controlScheme = pm->getCurProfile().getControlScheme();
			if(controlScheme < 1)
			{
				controlScheme = 5;
			}
			else
			{
				--controlScheme;
			}
			pm->getCurProfile().setControlScheme(controlScheme);
			Hologram::SetControlScheme((ControlScheme)(controlScheme));
		}
	}
	if (dI->KeyPressed(DIK_RIGHT) || dI->KeyPressed(DIK_D) || dI->JoystickDPadPressed(DIR_RIGHT) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 && controllerDelay <=0))
	{
		if(cursor == 104)
			controllerDelay = 0.3f;
		if (cursor == 104)
		{
			ProfileManager* pm = ProfileManager::GetInstance();
			int controlScheme = pm->getCurProfile().getControlScheme();
			if(controlScheme > 4)
			{
				controlScheme = 0;
			}
			else
			{
				++controlScheme;
			}
			pm->getCurProfile().setControlScheme(controlScheme);
			Hologram::SetControlScheme((ControlScheme)(controlScheme));
		}
	}


	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(	(ProfileManager::GetInstance()->getCurProfile().getMusicVolume() / 100.0f )
		* (ProfileManager::GetInstance()->getCurProfile().getMasterVolume()/100.0f));
	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(	(ProfileManager::GetInstance()->getCurProfile().getSFXVolume() / 100.0f)
		* (ProfileManager::GetInstance()->getCurProfile().getMasterVolume()/100.0f));
}

// Render
void OptionsMenuState::Render(void) const
{
	if( BackgroundImageID != -1 )
	{
		tM->Draw(BackgroundImageID, 0,0,0.95f,1.055f);
	}


	RECT renderRect;

	//Get the draw position based on screen width and height
	int ScreenWidth = Game::GetInstance()->GetScreenWidth();
	int ScreenHeight = Game::GetInstance()->GetScreenHeight();

	//Cal Draw Pos
	int DrawPosX = ScreenWidth / 2 - 350;
	int DrawPosY = ScreenHeight / 2 - 350;

	//Render the blue blue
	if ( NavBoxID != -1 )
	{
		if(Game::GetInstance()->GetScreenHeight() != 600 && Game::GetInstance()->GetScreenHeight() != 800)
			tM->Draw(NavBoxID, DrawPosX, DrawPosY);
		else
			tM->Draw(NavBoxID, DrawPosX-20, DrawPosY);
	}

#pragma region Menu Rendering
	for(int i =0; i < 4; ++i)
	{
		renderRect.top		 = 100 + 60*i + 20*i;
		renderRect.bottom	 = 160 + 60*i + 20*i;
		renderRect.left		 = 50; renderRect.right = 250;

		//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
		OffsetRect(&renderRect,DrawPosX,DrawPosY);

		if(this->ButtonID != -1  )
		{
			tM->Draw(ButtonID, renderRect.left, renderRect.top, 1.0f, 0.6f);
		}

		RECT volumeRect;
		switch(i)
		{
		case 0:
			BitmapFont::GetInstance()->Print("Master Volume:", (float)renderRect.left + 20, (float)renderRect.top + 25 ,0, D3DCOLOR_ARGB(255,0,0,0));
			volumeRect.top = renderRect.top;
			volumeRect.bottom = renderRect.bottom;
			volumeRect.left = renderRect.right + 50;
			volumeRect.right = volumeRect.left + 350;
			volumeRect.right =(LONG) Lerp((float)volumeRect.left,(float)volumeRect.right, ProfileManager::GetInstance()->getCurProfile().getMasterVolume()/100.0f);
			//OffsetRect(&volumeRect,DrawPosX,DrawPosY);

			D3D->DrawRect(volumeRect,D3DCOLOR_ARGB(255,0,0,255));
			break;
		case 1:
			BitmapFont::GetInstance()->Print("Music Volume:", (float)renderRect.left + 20, (float)renderRect.top  + 25,0, D3DCOLOR_ARGB(255,0,0,0));
			volumeRect.top = renderRect.top;
			volumeRect.bottom = renderRect.bottom;
			volumeRect.left = renderRect.right + 50;
			volumeRect.right = volumeRect.left + 350;
			volumeRect.right = (LONG)Lerp((float)volumeRect.left,(float)volumeRect.right, ProfileManager::GetInstance()->getCurProfile().getMusicVolume()/100.0f);
			//OffsetRect(&volumeRect,DrawPosX,DrawPosY);

			D3D->DrawRect(volumeRect,D3DCOLOR_ARGB(255,0,0,255));
			break;
		case 2:
			BitmapFont::GetInstance()->Print("SFX Volume:", (float)renderRect.left + 20, (float)renderRect.top + 25 ,0, D3DCOLOR_ARGB(255,0,0,0));
			volumeRect.top = renderRect.top;
			volumeRect.bottom = renderRect.bottom;
			volumeRect.left = renderRect.right + 50;
			volumeRect.right = volumeRect.left + 350;
			volumeRect.right = (LONG)Lerp((float)volumeRect.left,(float)volumeRect.right, ProfileManager::GetInstance()->getCurProfile().getSFXVolume()/100.0f);
			//OffsetRect(&volumeRect,DrawPosX,DrawPosY);

			D3D->DrawRect(volumeRect,D3DCOLOR_ARGB(255,0,0,255));
			break;
		case 3:
			BitmapFont::GetInstance()->Print("Windowed?", (float)renderRect.left  + 20, (float)renderRect.top + 25 ,0, D3DCOLOR_ARGB(255,0,0,0));
			break;
		}
	}

	//Windowed Box
	renderRect.top		= 345;
	renderRect.bottom	= 395;
	renderRect.left	= 300; 
	renderRect.right = 350;

	OffsetRect(&renderRect,DrawPosX,DrawPosY);

	D3D->DrawHollowRect(renderRect,D3DCOLOR_ARGB(255,0,0,0),6);

	if(ProfileManager::GetInstance()->getCurProfile().getIsWindowed())
	{
		D3D->DrawLine(renderRect.left,renderRect.top,renderRect.right,renderRect.bottom,D3DCOLOR_ARGB(255,0,0,0),6);
		D3D->DrawLine(renderRect.left,renderRect.bottom,renderRect.right,renderRect.top,D3DCOLOR_ARGB(255,0,0,0),6);
	}

	renderRect.top =		100 + 60*4 + 20*4;
	renderRect.bottom	 = 160 + 60*4 + 20*4;
	renderRect.left	 = 50; 
	renderRect.right = 250;

	OffsetRect(&renderRect,DrawPosX,DrawPosY);
	//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
	if(this->ButtonID != -1  )
	{
		tM->Draw(ButtonID, renderRect.left, renderRect.top, 1.0f, 0.6f);
	}
	BitmapFont::GetInstance()->Print("Controls:", (float)renderRect.left + 20, (float)renderRect.top + 25 ,0, D3DCOLOR_ARGB(255,0,0,0));

	RECT controlRect;
	controlRect.top	 = 	 105 + 60*4 + 20*4; controlRect.bottom = 165 + 60*4 + 20*4;
	controlRect.left = 300; 
	controlRect.right = 600;

	OffsetRect(&controlRect,DrawPosX,DrawPosY);

	int curControlScheme = ProfileManager::GetInstance()->getCurProfile().getControlScheme();
	switch(curControlScheme)
	{
	case 0:
		BitmapFont::GetInstance()->Print("WASD, JKL", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	case 1:
		BitmapFont::GetInstance()->Print("Arrow Keys, ZXC", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	case 2:
		BitmapFont::GetInstance()->Print("WASD, ZXC", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	case 3:
		BitmapFont::GetInstance()->Print("Arrow Keys, JKL", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	case 4:
		BitmapFont::GetInstance()->Print("XBOX Controller", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	case 5:
		BitmapFont::GetInstance()->Print("Arcade", (float)controlRect.left, (float)controlRect.top ,0, D3DCOLOR_ARGB(255,255,230,20));
		break;
	}

	renderRect.top = Game::GetInstance()->GetScreenHeight()-100;
	renderRect.bottom = Game::GetInstance()->GetScreenHeight()-20;
	renderRect.left = 5; renderRect.right = 200;

	if(Game::GetInstance()->GetScreenHeight() != 600 && Game::GetInstance()->GetScreenHeight() != 800)
		OffsetRect(&renderRect,DrawPosX,DrawPosY-350);
	else
		OffsetRect(&renderRect,DrawPosX,DrawPosY+80);

	if(this->ButtonID != -1  )
	{
		tM->Draw(ButtonID, renderRect.left, renderRect.top, 1.0f, 0.6f);
	}

	//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
	BitmapFont::GetInstance()->Print("Back:", (float)renderRect.left + 20, (float)renderRect.top + 25 ,0, D3DCOLOR_ARGB(255,0,0,0));

	RECT selectedRect;

	bool DrawBox = false;

	if(cursor < 100)
	{
		if(cursor < 4)
		{
			selectedRect.top	 = 100 + 60*cursor + 20*cursor;
			selectedRect.bottom	 = 160 + 60*cursor + 20*cursor;
			selectedRect.left	 = 50; 
			selectedRect.right	 = 250;
			OffsetRect(&selectedRect,DrawPosX,DrawPosY);
			DrawBox = false;

		}
		else if (cursor < 5)
		{
			selectedRect.top	 = 100 + 60*cursor + 20*cursor;
			selectedRect.bottom	 = 160 + 60*cursor + 20*cursor;
			selectedRect.left	 = 50; 
			selectedRect.right	 = 250;
			OffsetRect(&selectedRect,DrawPosX,DrawPosY);
			DrawBox = false;

		}
		else
		{
			selectedRect.top = Game::GetInstance()->GetScreenHeight()-100;
			selectedRect.bottom = Game::GetInstance()->GetScreenHeight()-20;
			selectedRect.left = 5; 
			selectedRect.right = 200;
			if(Game::GetInstance()->GetScreenHeight() != 600 && Game::GetInstance()->GetScreenHeight() != 800)
				OffsetRect(&selectedRect,DrawPosX,DrawPosY-350);
			else
				OffsetRect(&selectedRect,DrawPosX,DrawPosY+80);

			DrawBox = false;
		}
	}
	else
	{
		if(cursor < 104)
		{
			selectedRect.top	= 100 + 60*(cursor - 100) + 20*(cursor - 100);
			selectedRect.bottom = 160 + 60*(cursor - 100) + 20*(cursor - 100);
			selectedRect.left = 300;
			selectedRect.right = selectedRect.left + 350;
			OffsetRect(&selectedRect,DrawPosX,DrawPosY);
			DrawBox = true;
		}
		else
		{
			selectedRect.top	 = 100 + 60*(cursor - 100) + 20*(cursor - 100);
			selectedRect.bottom	 = 160 + 60*(cursor - 100) + 20*(cursor - 100);
			selectedRect.left	 = 300; selectedRect.right = 550;
			OffsetRect(&selectedRect,DrawPosX,DrawPosY);
			DrawBox = true;
		}
	}

	if(DrawBox == true)
	{
		D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);
	}
	else
	{
		if(ButtonSelID != -1 )
		{
			tM->Draw(ButtonSelID, selectedRect.left, selectedRect.top, 1.0f, 0.6f);
		}
	}
	

#pragma endregion


}

// Enter
//	- Used to initialize the game state.
void OptionsMenuState::Enter(void)
{
	GameState::Enter();
	BackgroundImageID = tM->LoadTexture("Textures\\Menu\\LevelSelectBackground.jpg");
	NavBoxID = tM->LoadTexture("Textures\\Menu\\LevelSelectNavBox.png");
	ButtonID = tM->LoadTexture("Textures\\Menu\\LevelSelBackButton.png");
	ButtonSelID = tM->LoadTexture("Textures\\Menu\\LevelSelButtonBackHighLight.png");
}

// Exit
//	- Used to clean up the game state.
void OptionsMenuState::Exit(void)
{
	GameState::Exit();
	DeleteInstance();
}


OptionsMenuState::OptionsMenuState(void)
{
	cursor = 0;
	inputDelay = 0.0f;
	controllerDelay = 0.0f;
	BackgroundImageID = -1;
	NavBoxID = -1;
	ButtonID = -1;
	ButtonSelID = -1;
}
OptionsMenuState::~OptionsMenuState(void)
{

}